<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of controller
 *
 * @author phamhphuc
 */

class referer_action_controller_admin extends system_controller_admin {    
    public function indexAction()
    {        
        header("location: ?action=userlist");
    }
    
    public function loginAction()
    {
        if($this->_isLogined()) {
            header("location: ?action=index");die;
        }
        if(isset($_POST['data'])){
            $data = $_POST['data'];
            $sql = "select * from #__admin_user where username = '{$data['username']}' and password = md5('{$data['password']}')";
            $user = $this->db->getRow($sql);
            if($user) {
                $this->session->user = $user;
                $this->db->update("#__admin_user", array(
                    "last_login" => date("Y-m-d H:i:s")
                ),"username = '{$user['username']}'");
                header("location: ?action=index");die;
            }
        }
    }   
    
    public function userlistAction()
    {
        $currentPage = 1;
        $start = 0;
        $limit = $this->config['facebooktab_item_per_page'];
        if(isset($_GET['p'])){
            $currentPage = intval($_GET['p']);
            if($currentPage <= 0){
                $currentPage = 1;
            }
        }
        
        $start = ($currentPage - 1) * $limit;

        $userlistSQL = "select u.*,iv.num_of_invite from #__user u left join (SELECT `invited_by_id` as user_id, count(*) as num_of_invite FROM `birken_invite_friend` group by invited_by_id) iv on u.user_id = iv.user_id limit {$start},{$limit}";
        $userlist = $this->db->getRows($userlistSQL);
        
//        print_r($userlist);
        
        $total = $this->db->getOne("select count(*) as total from #__user");
        $this->view->userlist = $userlist;
        $this->view->total = $total;
        $this->view->currentPage = $currentPage;
        $this->view->numOfPages = ceil($total/$limit);
    }
    
    public function logoutAction()
    {
        $this->session->user = false;
        header("location: ?action=login");die;
    }

    private function _isLogined()
    {
        if($this->session->user) {
            return true;
        } else {
            return false;
        }
    }

    private function _getCurrentUser($useCache = true)
    {
        if(!$useCache) {
            $user = $this->session->user;
            $sql = "select * from #__admin_user where user_id = {$user['user_id']}";
            $this->session->user = $this->db->getRow($sql);
        }
        return $this->session->user;

    }
    
    public function init()
    {
        parent::init();
        if($this->_isLogined()) {
            $user = $this->_getCurrentUser(false);
            if(!$user) {
                $this->session->user = false;
            } else {
                $this->view->user = $user;
                $this->view->isLogined = true;      
            }
        }      
        
    }

    
    private function _getFacebookObject()
    {
        require_once '../../libs/facebook/facebook.php';
        $facebook = new Facebook(array(
          'appId'  => $this->config['facebook_app_id'],
          'secret' => $this->config['facebook_app_secret'],
          'fileUpload' => true
        ));
        return $facebook;
    }
}

?>
